package com.dq.Week3.day12.linkedList.homework;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TextEditor {
    private List<String> lines;

    public TextEditor() {
        lines = new ArrayList<>();
    }

    public void insertText(int lineNum, String text) {
        if (lineNum <= 0 || lineNum > lines.size() + 1) {
            System.out.println("插入位置无效！");
            return;
        }
        lines.add(lineNum - 1, text);
    }

    public void printAll() {
        for (String line : lines) {
            System.out.println(line);
        }
    }

    public void deleteText(int lineNum) {
        if (lineNum <= 0 || lineNum > lines.size()) {
            System.out.println("删除位置无效！");
            return;
        }
        lines.remove(lineNum - 1);
    }

    public void modifyText(int lineNum, String newText) {
        if (lineNum <= 0 || lineNum > lines.size()) {
            System.out.println("修改位置无效！");
            return;
        }
        lines.set(lineNum - 1, newText);
    }

    public void insertTextAtSpecifiedLine(int lineNum, String text) {
        if (lineNum < 1 || lineNum > lines.size()) {
            System.out.println("指定行位置无效！");
            return;
        }
        lines.add(lineNum, text);
    }

    public static void main(String[] args) {
        TextEditor editor = new TextEditor();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("1.插入文本\t2.查询所有\t3.删除文本\t4.修改文本\t5.在指定行插入文本\t6.退出");
            System.out.print("请选择操作编号: ");
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.print("请输入要插入的行号: ");
                    int lineNum = scanner.nextInt();
                    System.out.print("请输入要插入的文本: ");
                    String text = scanner.nextLine();
                    editor.insertText(lineNum, text);
                    break;
                case 2:
                    editor.printAll();
                    break;
                case 3:
                    System.out.print("请输入要删除的行号: ");
                    lineNum = scanner.nextInt();
                    editor.deleteText(lineNum);
                    break;
                case 4:
                    System.out.print("请输入要修改的行号: ");
                    lineNum = scanner.nextInt();
                    System.out.print("请输入新的文本: ");
                    text = scanner.nextLine();
                    editor.modifyText(lineNum, text);
                    break;
                case 5:
                    System.out.print("请输入要在其之前插入新文本的行号: ");
                    lineNum = scanner.nextInt();
                    System.out.print("请输入要插入的文本: ");
                    text = scanner.nextLine();
                    editor.insertTextAtSpecifiedLine(lineNum, text);
                    break;
                case 6:
                    System.out.println("退出程序。");
                    System.exit(0);
                default:
                    System.out.println("无效的选择，请重新输入。");
            }
        }
    }
}